import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:tiktokadult/hj_page/mine_business/message/message_center_sub_page.dart';
import 'package:tiktokadult/hj_page/mine_business/message/widget/message_items.dart';
import 'package:tiktokadult/hj_utils/api_service/mine_service.dart';

abstract class MessageCenterSubLogic extends GetxController {
  final dataSource = [];
  int page = 1;
  bool isLoading = true;

  RefreshController? refreshController;

  @override
  void onReady() {
    super.onReady();
    fetchPageData();
  }

  @mustCallSuper
  fetchPageData({bool isRefresh = true, bool showLoading = false}) {
    if (isRefresh) {
      page = 1;
    }
    if (showLoading) {
      isLoading = true;
      update();
    }
  }

  Widget instanceChildItem(int index);
}

//私信
class MessageSessionLogic extends MessageCenterSubLogic {
  @override
  Widget instanceChildItem(int index) {
    return MessageSessionItem(dataSource[index]);
  }

  @override
  fetchPageData({bool isRefresh = true, bool showLoading = false}) async {
    super.fetchPageData(isRefresh: isRefresh, showLoading: showLoading);
    final res = await MineService.getUnReadChat(pageNumber: page);
    isLoading = false;
    if (isRefresh) {
      dataSource.clear();
      refreshController?.refreshCompleted();
    }
    if (res?.hasNext ?? false) {
      refreshController?.loadComplete();
    } else {
      refreshController?.loadNoData();
    }
    dataSource.addAll(res?.list ?? []);
    page += 1;
    update();
  }
}

//评论
class MessageCommentLogic extends MessageCenterSubLogic {
  @override
  Widget instanceChildItem(int index) {
    return MessageCommentItem(dataSource[index]);
  }

  @override
  fetchPageData({bool isRefresh = true, bool showLoading = false}) async {
    super.fetchPageData(isRefresh: isRefresh, showLoading: showLoading);
    final res = await MineService.getDynamicList(pageNumber: page, msgType: 2);
    isLoading = false;
    if (isRefresh) {
      dataSource.clear();
      refreshController?.refreshCompleted();
    }
    if (res?.hasNext ?? false) {
      refreshController?.loadComplete();
    } else {
      refreshController?.loadNoData();
    }
    dataSource.addAll(res?.list ?? []);
    page += 1;
    update();
  }
}

//点赞
class MessageThumbUpLogic extends MessageCenterSubLogic {
  @override
  fetchPageData({bool isRefresh = true, bool showLoading = false}) async {
    super.fetchPageData(isRefresh: isRefresh, showLoading: showLoading);
    final res = await MineService.getDynamicList(pageNumber: page, msgType: 1);
    isLoading = false;
    if (isRefresh) {
      dataSource.clear();
      refreshController?.refreshCompleted();
    }
    if (res?.hasNext ?? false) {
      refreshController?.loadComplete();
    } else {
      refreshController?.loadNoData();
    }
    dataSource.addAll(res?.list ?? []);
    page += 1;
    update();
  }

  @override
  Widget instanceChildItem(int index) {
    return MessageThumbItem(dataSource[index]);
  }
}

MessageCenterSubLogic instanceController(MessageType type) {
  switch (type) {
    case MessageType.comment:
      return MessageCommentLogic();
    case MessageType.session:
      return MessageSessionLogic();
    case MessageType.thumb:
      return MessageThumbUpLogic();
    default:
      throw '未知的类型${type}';
  }
}
